﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using Micrhard.FMCG.Server.Models;
using Micrhard.FMCG.Server.Msg;
using Micrhard.FMCG.Server.Interface;
using Microhard.Core;
using FluentValidation.Results;

namespace Micrhard.FMCG.Server.Validator
{
    public class OrderDetailValidator : AbstractValidator<OrderDetailModelInServer>
    {
        private IOrderDetail iod = IoC.Resolve<IOrderDetail>();

        public OrderDetailValidator()
        {
            RuleSet("Insert", () =>
            {
                RuleFor(u => u.SKU).NotEmpty().WithMessage(OrderMsg.NeedPrd).When(r => r.OrderDetailID == Guid.Empty);
                RuleFor(u => u.OrderQty).GreaterThan(0).WithMessage(OrderMsg.NeedQty).When(r => r.OrderDetailID == Guid.Empty);
                RuleFor(u => u.OrderID).NotEmpty().WithMessage(OrderMsg.NeedOrderID).When(r => r.OrderDetailID == Guid.Empty);

                Custom(u =>
                {                 
                    if (iod.IsCostCoinEnough(u))
                    {
                        return new ValidationFailure("SKU", OrderMsg.MaxCostCoin);
                    }
                    return null;
                });
            });

            RuleSet("Update", () =>
            {
                RuleFor(u => u.SKU).NotEmpty().WithMessage(OrderMsg.NeedPrd);
                RuleFor(u => u.OrderQty).GreaterThan(0).WithMessage(OrderMsg.NeedQty);
                RuleFor(u => u.OrderID).NotEmpty().WithMessage(OrderMsg.NeedOrderID);

                Custom(u =>
                {
                    if (iod.IsCostCoinEnough(u))
                    {
                        return new ValidationFailure("SKU", OrderMsg.MaxCostCoin);
                    }
                    return null;
                });
            });
        }
    }
}
